package com.hellogood.yaohuo.server.mgr;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.PdDetail;
import com.hellogood.yaohuo.shared.entity.PdForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class PandianMgr {
	Logger log = Logger.getLogger(this.getClass());

	public String createPandianForm(PdForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPdFormDAO().createPandian(form, false);
		if (result.equals("")) {
			addLog(form.getPdBillNo(), false);
		}
		return result;
	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改盘点单：" + billNo);
		} else {
			log.setContent("新建盘点单：" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public String modifyPandianForm(PdForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPdFormDAO().createPandian(form, true);
		if (result.equals("")) {
			addLog(form.getPdBillNo(), true);
		}
		return result;
	}

	private void execute(PdForm form) {
		List<PdDetail> list = form.getList();
		int totalCounts = 0;
		BigDecimal formCost = new BigDecimal(0);
		BigDecimal formTotal = new BigDecimal(0);

		for (PdDetail d : list) {
			BigDecimal ykCost = d.getjPrice().multiply(new BigDecimal(d.getYkCounts()));
			BigDecimal ykTotal = d.getPrice().multiply(new BigDecimal(d.getYkCounts()));
			d.setYkCost(ykCost);
			d.setYkTotal(ykTotal);
			totalCounts += d.getYkCounts();
			formCost = formCost.add(ykCost);
			formTotal = formTotal.add(ykTotal);
		}
		form.setNetCost(formCost);
		form.setYkCounts(totalCounts);
		form.setYkCost(formCost);
		form.setYkTotal(formTotal);
	}

	public int getLastPdBillNo(String billNo) {
		String no = DAOFactory.getInstance().createPdFormDAO().getLastPdBillNo(billNo);
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			if (no.length() > 2) {
				String lastNo = no.substring(no.length() - 2, no.length());
				return Integer.valueOf(lastNo);
			}
		}
		return 0;
	}

	public List<PdForm> searchPdForm(String code, int level, String startDate, String endDate, String rz) {
		if (code.equals("美季")) {
			level = 1;
		} else if (code.equals("C002")) {
			level = 3;
		}

		if (level == 2) {
			return DAOFactory.getInstance().createPdFormDAO().searchPdFormByQuyu(code, startDate, endDate, rz);
		} else if (level == 3) {
			return DAOFactory.getInstance().createPdFormDAO().searchPdForm(code, startDate, endDate, rz);
		} else if (level == 1) {
			return DAOFactory.getInstance().createPdFormDAO().searchPdFormByQuyu(null, startDate, endDate, rz);
		}
		return null;
	}

	public PdForm getFormDetail(String billNo) {
		PdForm form = DAOFactory.getInstance().createPdFormDAO().getFormByBillNo(billNo);
		List<PdDetail> list = DAOFactory.getInstance().createPdDetailDAO().getDetailByBillNo(billNo);
		form.setList(list);
		return form;
	}

	public String deletePandian(String billNo) {
		return DAOFactory.getInstance().createPdFormDAO().deletePandian(billNo);
	}

	public String rz(List<String> bills) {
		String result = "";
		try {
			for (String bill : bills) {
				DAOFactory.getInstance().createPdFormDAO().rz(bill);
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			result = e.getMessage();
		}
		return result;
	}
}
